package com.manong.bookstore.search.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manong.common.utils.R;
import com.manong.bookstore.search.feign.UserFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27
 */

@RefreshScope // 使得Nacos Config的实时改动可以生效
@RestController
@RequestMapping("search/user")
public class UserController {
    // 查询用户的初始化方法
    @RequestMapping("/hello")
    public R hello(){
        return R.ok().put("search", "user,address,collect");
    }

    @Autowired
    private UserFeignService userFeignService;

    // 调用远程服务user的list方法
    // 展示调用user的所有结果
    @RequestMapping("/user/list")
    public R listUser(){
        R list = userFeignService.listUser();
        return R.ok().put("users", list);
    }

    // 对user某一项的值进行升序或降序查询
    @RequestMapping("/user/sequence")
    public R sequenceUser(@RequestParam String key, @RequestParam(defaultValue = "true") Boolean isAsc){
        R list = userFeignService.listUser();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        if(!jsonArray.isEmpty()) {
            if(isAsc) {
                try {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getBigDecimal(key)));
                } catch (Exception e) {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString(key)));
                }
            } else {
                try {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getBigDecimal(key)).reversed());
                } catch (Exception e) {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString(key)).reversed());
                }
            }
        }
        int rows = jsonArray.size(); // 记录返回结果
        jsonObject.put("rows", rows);
        return R.ok().put("sequence", jsonObject);
    }

    // 对user某一项的值进行精准查询
    @RequestMapping("/user/accurate")
    public R accurateUser(@RequestParam String key, @RequestParam String value){
        R list = userFeignService.listUser();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0; // 记录返回结果
        if(!jsonArray.isEmpty()) {
            try {
                jsonArray = jsonArray.stream().filter(
                    obj -> value.equals(((JSONObject) obj).getString(key))
                ).collect(Collectors.toCollection(JSONArray::new));
            } catch (Exception e) {
                jsonArray = jsonArray.stream().filter(
                    obj -> value.equals(((JSONObject) obj).getBigDecimal(key).toString())
                ).collect(Collectors.toCollection(JSONArray::new));
            }
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("accurate", jsonObject);
    }

    // 对user某一项的值进行模糊查询
    @RequestMapping("/user/fuzzy")
    public R fuzzyUser(@RequestParam String key, @RequestParam String value){
        R list = userFeignService.listUser();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0;
        if(!jsonArray.isEmpty()) {
            try {
                jsonArray = jsonArray.stream().filter(
                    obj -> (((JSONObject) obj).getString(key)).contains(value)
                ).collect(Collectors.toCollection(JSONArray::new));
            } catch (Exception e) {
                jsonArray = jsonArray.stream().filter(
                    obj -> ((JSONObject) obj).getBigDecimal(key).toString().contains(value)
                ).collect(Collectors.toCollection(JSONArray::new));
            }
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("fuzzy", jsonObject);
    }

    // 调用远程服务address的list方法
    // 展示调用address的所有结果
    @RequestMapping("/address/list")
    public R listAddress(){
        R list = userFeignService.listAddress();
        return R.ok().put("addresses", list);
    }

    // 对address某一项的值进行升序或降序查询
    @RequestMapping("/address/sequence")
    public R sequenceAddress(@RequestParam String key, @RequestParam(defaultValue = "true") Boolean isAsc){
        R list = userFeignService.listAddress();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        if(!jsonArray.isEmpty()) {
            if(isAsc) {
                try {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInteger(key)));
                } catch (Exception e) {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString(key)));
                }
            } else {
                try {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInteger(key)).reversed());
                } catch (Exception e) {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString(key)).reversed());
                }
            }
        }
        int rows = jsonArray.size(); // 记录返回结果
        jsonObject.put("rows", rows);
        return R.ok().put("sequence", jsonObject);
    }

    // 对addressr某一项的值进行精准查询
    @RequestMapping("/address/accurate")
    public R accurateAddress(@RequestParam String key, @RequestParam String value){
        R list = userFeignService.listAddress();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0; // 记录返回结果
        if(!jsonArray.isEmpty()) {
            try {
                jsonArray = jsonArray.stream().filter(
                        obj -> value.equals(((JSONObject) obj).getString(key))
                ).collect(Collectors.toCollection(JSONArray::new));
            } catch (Exception e) {
                jsonArray = jsonArray.stream().filter(
                        obj -> value.equals(((JSONObject) obj).getInteger(key).toString())
                ).collect(Collectors.toCollection(JSONArray::new));
            }
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("accurate", jsonObject);
    }

    // 对address某一项的值进行模糊查询
    @RequestMapping("/address/fuzzy")
    public R fuzzyAddress(@RequestParam String key, @RequestParam String value){
        R list = userFeignService.listAddress();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0;
        if(!jsonArray.isEmpty()) {
            try {
                jsonArray = jsonArray.stream().filter(
                        obj -> (((JSONObject) obj).getString(key)).contains(value)
                ).collect(Collectors.toCollection(JSONArray::new));
            } catch (Exception e) {
                jsonArray = jsonArray.stream().filter(
                        obj -> ((JSONObject) obj).getInteger(key).toString().contains(value)
                ).collect(Collectors.toCollection(JSONArray::new));
            }
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("fuzzy", jsonObject);
    }

    // 调用远程服务collect的list方法
    // 展示调用collect的所有结果
    @RequestMapping("/collect/list")
    public R listCollect(){
        R list = userFeignService.listCollect();
        return R.ok().put("collects", list);
    }

    // 对collect某一项的值进行升序或降序查询
    @RequestMapping("/collect/sequence")
    public R sequenceCollect(@RequestParam String key, @RequestParam(defaultValue = "true") Boolean isAsc){
        R list = userFeignService.listCollect();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        if(!jsonArray.isEmpty()) {
            if(isAsc) {
                jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInteger(key)));
            } else {
                jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInteger(key)).reversed());
            }
        }
        int rows = jsonArray.size(); // 记录返回结果
        jsonObject.put("rows", rows);
        return R.ok().put("sequence", jsonObject);
    }

    // 对collect某一项的值进行精准查询
    @RequestMapping("/collect/accurate")
    public R accurateCollect(@RequestParam String key, @RequestParam String value){
        R list = userFeignService.listCollect();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0; // 记录返回结果
        if(!jsonArray.isEmpty()) {
            jsonArray = jsonArray.stream().filter(
                    obj -> value.equals(((JSONObject) obj).getInteger(key).toString())
            ).collect(Collectors.toCollection(JSONArray::new));
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("accurate", jsonObject);
    }

    // 对collect某一项的值进行模糊查询
    @RequestMapping("/collect/fuzzy")
    public R fuzzyCollect(@RequestParam String key, @RequestParam String value){
        R list = userFeignService.listCollect();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0;
        if(!jsonArray.isEmpty()) {
            jsonArray = jsonArray.stream().filter(
                    obj -> ((JSONObject) obj).getInteger(key).toString().contains(value)
            ).collect(Collectors.toCollection(JSONArray::new));
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("fuzzy", jsonObject);
    }

    // 配置中心功能获取application.properities中的内容
    @Value("${search.user.name}")
    private String username;
    @Value("${search.user.age}")
    private Integer userage;
    @RequestMapping("/testNacosConfig")
    public R testNacosConfig() {
        return R.ok().put("username",username).put("userage",userage);
    }

}
